import ArgumentType from './extension-support/argument-type';
import BlockType from './extension-support/block-type';
import blockIconURI from './image/icon.svg';
// 导入翻译模块的方法
let { setLocaleData, formatMessage } = require('./translation/index');
// 导入translation/locales/index.js
import locales from './translation/locales';
// 初始化翻译内容
setLocaleData(locales);


class YourExtension{
    constructor(runtime) {
        this.runtime = runtime;
        // 绑定获取语言环境的方法, 这个步骤不可缺少
        formatMessage = formatMessage.bind(null, runtime.getLocale);
    }

    getInfo () {
        return {
            name: 'json',
            color:"#ff9933",
            blockIconURI: blockIconURI,
            blocks: [
                {  
                    opcode: 'getJson',
                    blockType: BlockType.REPORTER,
                    text: formatMessage({
                        id: 'getJsonText',
                        default: 'Get the value of json [NUM1] key[NUM2]. '
                    }), 
                    arguments: {
                        NUM1: {
                            type: ArgumentType.STRING,
                            defaultValue: "json"
                        },
                        NUM2: {
                            type: ArgumentType.STRING,
                            defaultValue: "key"
                        }
                    }
                },
                {  
                    opcode: 'getJson1',
                    blockType: BlockType.REPORTER,
                    text: formatMessage({ 
                        id: 'getJson1Text',
                        default:'Get the value of json [NUM1] multi-layer key[NUM2].'
                    }),
                    arguments: {
                        NUM1: {
                            type: ArgumentType.STRING,
                            defaultValue: "json"
                        },
                        NUM2: {
                            type: ArgumentType.STRING,
                            defaultValue: "[\"data\"][\"key\"]"
                        }
                    }
                }
            ],
            menus: {
                operator: ['+', '-', '*', '/']
            }
        }
    }
    // block运行函数, 与opcode同名
    getJson(args){
        const num1 = args.NUM1;
        const num2 = args.NUM2;
        let json = JSON.parse(num1)
        return json[num2]


    }
    // block运行函数, 与opcode同名
    getJson1(args){
        const num1 = args.NUM1;
        const num2 = args.NUM2;
        let json = JSON.parse(num1)
        return eval(`json${num2}`)


    }

}
module.exports = YourExtension;